package com.boc.langxiang.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * Created by Li Guoxi on 2016/2/16.
 */
public abstract class CommonAdapter<T> extends BaseAdapter {

    protected LayoutInflater inflater;
    protected Context context;
    protected List<T> list;

    protected final int itemLayoutID;

    public CommonAdapter(int itemLayoutID, List<T> list, Context context) {
        this.itemLayoutID = itemLayoutID;
        this.list = list;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final CommonViewHolder viewHolder = getViewHolder(position, convertView, parent);
        convert(viewHolder, list.get(position), position);
        return viewHolder.getConvertView();
    }

    private CommonViewHolder getViewHolder(int position, View convertView, ViewGroup parent) {
        return CommonViewHolder.get(context, convertView, parent, itemLayoutID, position);
    }

    public abstract void convert(CommonViewHolder viewHolder, T item, int position);


    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
}
